﻿package com.game.view.layer
{
	import com.dusk.util.FilterUtil;
	import com.dusk.util.ResUtil;
	import com.dusk.util.TextUtil;
	import com.game.engine.MainGame;
	import com.game.event.GameEventType;
	import com.game.manager.EventManager;
	import com.game.manager.GameSceneManager;
	import com.game.manager.SettingManager;
	import com.game.manager.SoundManager;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class SetMenu extends LayerBase
	{
		
		public function SetMenu()
		{
			super();
		}
		
		private var close:SimpleButton;
		private var backToMap:TextField;
		private var gameHelp:TextField;
		private var frameRate:TextField;
		private var controlSnd:TextField;
		private var backToMenu:TextField;
		
		override protected function beforeAdded():void
		{
			var mc:MovieClip = ResUtil.getLayerView("SetMenu") as MovieClip;
			TextUtil.redrawTxt(mc);
			close = mc.getChildByName("close") as SimpleButton;
			backToMap = mc.getChildByName("backToMap") as TextField;
			gameHelp = mc.getChildByName("gameHelp") as TextField;
			frameRate = mc.getChildByName("frameRate") as TextField;
			controlSnd = mc.getChildByName("controlSnd") as TextField;
			backToMenu = mc.getChildByName("backToMenu") as TextField;
			addChild(mc);
		}
		
		override protected function onAdded():void
		{
			backToMap.addEventListener(MouseEvent.CLICK, backToMapClick);
			backToMap.addEventListener(MouseEvent.ROLL_OVER, react);
			backToMap.addEventListener(MouseEvent.ROLL_OUT, react);
			
			gameHelp.addEventListener(MouseEvent.CLICK, helpClick);
			gameHelp.addEventListener(MouseEvent.ROLL_OVER, react);
			gameHelp.addEventListener(MouseEvent.ROLL_OUT, react);
			
			frameRate.addEventListener(MouseEvent.CLICK, frameRateClick);
			frameRate.addEventListener(MouseEvent.ROLL_OVER, react);
			frameRate.addEventListener(MouseEvent.ROLL_OUT, react);
			
			controlSnd.addEventListener(MouseEvent.CLICK, closeSound);
			controlSnd.addEventListener(MouseEvent.ROLL_OVER, react);
			controlSnd.addEventListener(MouseEvent.ROLL_OUT, react);
			
			backToMenu.addEventListener(MouseEvent.CLICK, backToMenuClick);
			backToMenu.addEventListener(MouseEvent.ROLL_OVER, react);
			backToMenu.addEventListener(MouseEvent.ROLL_OUT, react);
			
			close.addEventListener(MouseEvent.CLICK, closeClick);
			EventManager.getIns().registerListen(GameEventType.CLOSE_SET_MENU, closeClick);
			init();
		}
		
		override protected function onRemoved():void
		{
			backToMap.removeEventListener(MouseEvent.CLICK, backToMapClick);
			backToMap.removeEventListener(MouseEvent.ROLL_OVER, react);
			backToMap.removeEventListener(MouseEvent.ROLL_OUT, react);
			
			gameHelp.removeEventListener(MouseEvent.CLICK, helpClick);
			gameHelp.removeEventListener(MouseEvent.ROLL_OVER, react);
			gameHelp.removeEventListener(MouseEvent.ROLL_OUT, react);
			
			frameRate.removeEventListener(MouseEvent.CLICK, frameRateClick);
			frameRate.removeEventListener(MouseEvent.ROLL_OVER, react);
			frameRate.removeEventListener(MouseEvent.ROLL_OUT, react);
			
			controlSnd.removeEventListener(MouseEvent.CLICK, closeSound);
			controlSnd.removeEventListener(MouseEvent.ROLL_OVER, react);
			controlSnd.removeEventListener(MouseEvent.ROLL_OUT, react);
			
			backToMenu.removeEventListener(MouseEvent.CLICK, backToMenuClick);
			backToMenu.removeEventListener(MouseEvent.ROLL_OVER, react);
			backToMenu.removeEventListener(MouseEvent.ROLL_OUT, react);
			
			close.addEventListener(MouseEvent.CLICK, closeClick);
			EventManager.getIns().removeListen(GameEventType.CLOSE_SET_MENU, closeClick);
		}
		
		private function react(evt:MouseEvent):void
		{
			var tf:TextFormat;
			switch (evt.type)
			{
				case MouseEvent.ROLL_OVER:
					tf = evt.currentTarget.defaultTextFormat;
					tf.color = 0xFFFF00;
					evt.currentTarget.setTextFormat(tf);
					evt.currentTarget.defaultTextFormat = tf;
					FilterUtil.setGlow(evt.currentTarget,0xFFFF00,0.75,5,5,2,3);
					FilterUtil.clearCache();
					break;
				case MouseEvent.ROLL_OUT:
					tf = evt.currentTarget.defaultTextFormat;
					tf.color = 0xFFFFFF;
					evt.currentTarget.setTextFormat(tf);
					evt.currentTarget.defaultTextFormat = tf;
					FilterUtil.deleteAllFilters(evt.currentTarget);
					break;
			}
		}
		
		private function init():void
		{
			if (SoundManager.currentVolume > 0)
			{
				controlSnd.text = "关闭声音";
			}
			else
			{
				controlSnd.text = "打开声音";
			}
			frameRate.text = "帧率 : " + gc.frameClips.toString();
		}
		
		private function backToMapClick(param1:MouseEvent):void
		{
			MainGame.getIns().destroyGame();
			GameSceneManager.getIns().switchScene(GameSceneManager.GAME_MAP);
		}
		
		private function backToMenuClick(param1:MouseEvent):void
		{
			MainGame.getIns().destroyGame();
			GameSceneManager.getIns().switchScene("GameMenu");
		}
		
		private function helpClick(param1:MouseEvent):void
		{
			GameSceneManager.getIns().addScene(new GameHelp());
		}
		
		private function frameRateClick(param1:MouseEvent):void
		{
			if(gc.frameClips >= 90)
			{
				gc.frameClips = 30;
			}
			else
			{
				gc.frameClips += 30;
			}
			frameRate.text = "帧率 : " + gc.frameClips.toString();
			SettingManager.getSettingVO().battleFPS = gc.frameClips;
		}
		
		private function closeSound(param1:MouseEvent):void
		{
			var vol:int = SoundManager.currentVolume > 0 ? 0 : 50;
			SoundManager.setVolume(vol);
			if (SoundManager.currentVolume > 0)
			{
				controlSnd.text = "关闭声音";
			}
			else
			{
				controlSnd.text = "打开声音";
			}
		}
		
		private function closeClick(evt:MouseEvent = null):void
		{
			removeFromParent();
			EventManager.getIns().dispatchEvent(GameEventType.CONTINUE_GAME);
		}
	}
}
